﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.IO;

namespace dzs.Web.supply
{
    public partial class ListMonthSupplyPlan : System.Web.UI.Page
    {
        Maticsoft.BLL.monthSupplayPlan msm = new Maticsoft.BLL.monthSupplayPlan();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;

                    int roles_id = Convert.ToInt32(ds.Tables[0].Rows[0]["roles_id"].ToString());
                    int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                    Bind(user_id);
                }
                //Bind(11);
            }
        }

        private void Bind(int user_id)
        {
            StringBuilder str = new StringBuilder();

            str.Append(" user_id=" + user_id + " and status = '1' ");

            if (!string.IsNullOrEmpty(txt_material.Text))
            {
                str.Append(" and material like '%" + txt_material.Text + "%' ");
            }
            if (!string.IsNullOrEmpty(txt_projectName.Text))
            {
                str.Append(" and projectName like '%" + txt_projectName.Text + "%' ");
            }
            if (!string.IsNullOrEmpty(txt_ghqStart.Text))
            {
                str.Append(" and supplyDate >='" + txt_ghqStart.Text + "' ");
            }
            if (!string.IsNullOrEmpty(txt_ghqEnd.Text))
            {
                str.Append(" and supplyDate <='" + txt_ghqEnd.Text + "' ");
            }


            DataSet ds = msm.GetListMonthSupplyInfo(str.ToString());
            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
            List<dzs.Model.configinfo> cm = new dzs.BLL.configinfo().GetModelList("");
            if (cm.Count == 1)
            {
                dzs.Model.configinfo c = cm[0];
                this.lbl_se.Text = "*注：当月供应计划起始日期：本月" + c.ygyqs + "号，截止日期：下月" + c.ygyjs + "号";
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //if (e.CommandName == "lbn_add")
            //{
            //    if (e.CommandArgument != null)
            //    {
            //        //Session["supplyPlanIdAddMonth"] = e.CommandArgument.ToString();

            //        //ClientScript.RegisterStartupScript(this.GetType(), "", "<script>OpenPop('FrameAddMonthSupplyInfo.aspx','900px','390px');</script>");
            //    }
            //}
            if (e.CommandName == "outconfirm")
            {
                GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
                string unit, proname, id, jdate, jhnum, wl;//需用单位，项目名称，采购订单号，合同交货期，招标批次，货物名称
                //int pid = int.Parse(gwSearch.DataKeys[gvr.RowIndex].Value.ToString());                
                jhnum = gvr.Cells[4].Text.Trim();
                wl = gvr.Cells[10].Text.Trim();
                id = gvr.Cells[5].Text.Trim();
                unit = ((Label)gvr.Cells[9].Controls[1]).Text;
                jdate = ((Label)gvr.Cells[14].Controls[1]).Text;
                proname = gvr.Cells[7].Text.Trim();
                //name = ((Label)gvr.Cells[25].Controls[1]).Text;
                //fax = ((Label)gvr.Cells[26].Controls[1]).Text;
                string file = ExWord.ExportToWordconfirm(unit, jdate, jhnum, proname, id, wl);
                FileInfo DownloadFile = new FileInfo(file);
                Response.Clear();
                Response.ClearHeaders();
                Response.Buffer = false;
                Response.ContentType = "application/octet-stream";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.Name, System.Text.Encoding.UTF8));
                Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
                Response.WriteFile(DownloadFile.FullName);
                Response.Flush();
                Response.End();

            }
            if (e.CommandName == "outex")
            {
                //int index = int.Parse(e.CommandArgument.ToString());
                GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
                string unit, proname, id, jdate, jhnum, wl;
                //int pid = int.Parse(gwSearch.DataKeys[gvr.RowIndex].Value.ToString());
                jhnum = gvr.Cells[4].Text.Trim();
                wl = gvr.Cells[10].Text.Trim();
                id = gvr.Cells[5].Text.Trim();
                unit = ((Label)gvr.Cells[9].Controls[1]).Text;
                jdate = ((Label)gvr.Cells[14].Controls[1]).Text;
                proname = gvr.Cells[7].Text.Trim();
                string file = ExWord.ExportToWordExped(unit, jdate, jhnum, proname, id, wl);
                FileInfo DownloadFile = new FileInfo(file);
                Response.Clear();
                Response.ClearHeaders();
                Response.Buffer = false;
                Response.ContentType = "application/octet-stream";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.Name, System.Text.Encoding.UTF8));
                Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
                Response.WriteFile(DownloadFile.FullName);
                Response.Flush();
                Response.End();

            }

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;

            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;

                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                Bind(user_id);
            }

        }

        protected void btn_reload_Click(object sender, EventArgs e)
        {
            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;

                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                Bind(user_id);
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                        e.Row.Cells[i].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                }

                //Label lbl_supplyDate = e.Row.FindControl("lbl_supplyDate") as Label;//供应年月

                //List<dzs.Model.configinfo> cm = new dzs.BLL.configinfo().GetModelList("");

                //if (cm.Count == 1)
                //{
                //    dzs.Model.configinfo c = cm[0];

                //    //string monthStartTime = DateTime.Today.AddMonths(-1).ToString("yyyy-MM-" + c.ygyqs + "");//月供应计划起始时间
                //    //string monthEndTime = DateTime.Today.ToString("yyyy-MM-" + c.ygyjs + "");//月供应计划结束时间

                //    if (!string.IsNullOrEmpty(lbl_supplyDate.Text))
                //    {
                //        DateTime supplyDate = DateTime.Parse(lbl_supplyDate.Text);//供应年月
                //        DateTime monthEndTime;//月供应计划结束时间
                //        DateTime monthStartTime;//月供应计划起始时间


                //        int day = DateTime.Today.Day;

                //        if (day < c.ygyjs)
                //        {
                //            monthStartTime = DateTime.Parse(DateTime.Today.AddMonths(-1).ToString("yyyy-MM-" + c.ygyqs + ""));
                //            monthEndTime = DateTime.Parse(DateTime.Today.ToString("yyyy-MM-" + c.ygyjs + ""));
                //        }
                //        else
                //        {
                //            monthStartTime = DateTime.Parse(DateTime.Today.ToString("yyyy-MM-" + c.ygyqs + ""));
                //            monthEndTime = DateTime.Parse(DateTime.Today.AddMonths(1).ToString("yyyy-MM-" + c.ygyjs + ""));
                //        }

                      
                //        if (DateTime.Compare(supplyDate, monthEndTime) < 0 && DateTime.Compare(supplyDate, monthStartTime) > 0)//供应年月大于起始时间，且小于结束时间
                //        {
                //            e.Row.Visible = true;
                //        }
                //        else
                //        {
                //            e.Row.Visible = false;
                //        }

                //    }

                //}

            }
        }

        protected void btn_sel_Click(object sender, EventArgs e)
        {
            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;

                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                Bind(user_id);
            }
        }

        //导出复写
        public override void VerifyRenderingInServerForm(Control control)       //导出
        { }
        //导出excel
        protected void btn_out_Click(object sender, EventArgs e)
        {
            this.GridView1.AllowPaging = false;

            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;

                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                Bind(user_id);
            }

            if (this.GridView1.Rows.Count != 0)
            {
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "GB2312";
                Response.AppendHeader("Content-Disposition", "attachment;filename=dygyjhb.xls");
                //Response.ContentEncoding = System.Text.Encoding.UTF7;
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                Response.ContentType = "application/ms-excel";
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                Response.Output.Write("<H4 align='center'>当月供应计划到货情况表</H4>");
                this.GridView1.RenderControl(oHtmlTextWriter);
                Response.Output.Write(oStringWriter.ToString());
                Response.Flush();
                Response.End();

            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('数据为空，无法导出表格')</script>");

            }

        }

    }
}